package com.koocloud.electroplatemanage.service.impl;

import com.koocloud.electroplatemanage.auth.mapper.IUserDao;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.*;
import com.koocloud.electroplatemanage.pojo.*;
import com.koocloud.electroplatemanage.service.QrCodeService;
import com.koocloud.electroplatemanage.utils.Base64Util;
import com.koocloud.electroplatemanage.utils.qrcodeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: electroplatemanage
 * @description: 二维码服务层实现类
 * @author: zww
 * @create: 2020-11-04 13:23
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class QrCodeServiceImpl implements QrCodeService {
    @Resource
    private ProductionPlanMapper productionPlanMapper;
    @Resource
    private IncomePartUnitMapper incomePartUnitMapper;
    @Resource
    private DrawNoMapper drawNoMapper;
    @Resource
    private IncomePartJudgeMapper  incomePartJudgeMapper;
    @Resource
    private IncomeBasicInfoMapper incomeBasicInfoMapper;
    Lock lock = new ReentrantLock();
    /**
     *@Description:查询零件二维码(厂家、零件数量、型号、项目名称、零件名称、图号、图纸版次、批次号、订单号)
     *@Param: [batchNumber, orderNumber]批次号，订单号
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: zww
     *@date: 2020/11/4
     */
    @Override
    public ResponseTemplate selectQrCodeBase64(String workOrderNumber) {
        try {
            /*IncomePartJudgeVo incomePartJudgeVo = null;
            IncomeBasicInfoVo incomeBasicInfoVo = null;
            if (!StringUtils.isEmpty(workOrderNumber)) {
                StringBuilder sb =new StringBuilder();
                String str = workOrderNumber.substring(0,17) + "A";
                sb.append(str);
                //根据工单号查询台账表记录
                incomePartJudgeVo = incomePartJudgeMapper.selectByWorkOrderNumber(sb.toString());
                incomeBasicInfoVo = incomeBasicInfoMapper.selectIncomeBasicInfo(sb.toString());
            } else {
                workOrderNumber = "";//工单号
            }

            String incomePartUnit = "";//厂家
            String partNum = "";//零件数量
            String modelNumber = "";//型号
            String projectName = "";//项目名称
            String partName = "";//零件名称
            String drawNo = "";//图号
            String drawRevision = "";//图纸版次
            String orderNumber = "";//订单号

            if (!StringUtils.isEmpty(incomePartJudgeVo)) {
                if (!StringUtils.isEmpty(incomePartJudgeVo.getIncomePartUnitCode())) {
                    IncomePartUnitVo incomePartUnitVo = incomePartUnitMapper.selectByPk(incomePartJudgeVo.getIncomePartUnitCode());
                    if (!StringUtils.isEmpty(incomePartUnitVo)) {
                        //设置来件单位名称
                        incomePartUnit = incomePartUnitVo.getCnName();
                    }
                }

                if (!StringUtils.isEmpty(incomePartJudgeVo.getIncomePartQuantity())) {
                    //设置来件数量
                    partNum = incomePartJudgeVo.getIncomePartQuantity();
                }

                if (!StringUtils.isEmpty(incomePartJudgeVo.getModelNumber())) {
                    //设置型号
                    modelNumber = incomePartJudgeVo.getModelNumber();
                }
                if (!StringUtils.isEmpty(incomePartJudgeVo.getPartName())) {
                    //设置零件名称
                    partName = incomePartJudgeVo.getPartName();
                }
                if (!StringUtils.isEmpty(incomePartJudgeVo.getOrderNumber())) {
                    //设置订单号
                    orderNumber = incomePartJudgeVo.getOrderNumber();
                }
                if (!StringUtils.isEmpty(incomeBasicInfoVo)) {
                    if(!StringUtils.isEmpty(incomeBasicInfoVo.getDrawEdition())) {
                        //设置图纸版次
                        drawRevision = incomeBasicInfoVo.getDrawEdition();
                    }
                }
                if (!StringUtils.isEmpty(incomePartJudgeVo.getDrawNoCode())) {
                    DrawNoVo drawNoVo = drawNoMapper.selectByPk(incomePartJudgeVo.getDrawNoCode());
                    if (!StringUtils.isEmpty(drawNoVo)) {
                        //设置图号名称
                        drawNo = drawNoVo.getName();
                    }
                }


            }

            // 存放在二维码中的内容
            String qrCodeText = "厂家:" + incomePartUnit + "、零件数量:" + partNum + "、型号:" + modelNumber +
                    "、项目名称:" + projectName + "、零件名称:" + partName + "、图号:" + drawNo + "、图纸版次:" + drawRevision +
                    "、批次号:" + orderNumber + "、工单号:" + workOrderNumber;*/
            String qrCodeText = Base64Util.encode(workOrderNumber);
            return ResponseTemplate.builder().code(0).message("成功").data(qrCodeText).count(0l).build();
        } catch (Exception e) {
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

}